package com.qding.park.manager.rest;

import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.qding.park.manager.rest.filter.SimpleCORSFilter;

@Configuration
@ComponentScan(basePackages = { "com.qding"})
@ServletComponentScan
@ImportResource({ "classpath:disconf.xml" }) // 引入disconf
@PropertySource({"classpath:application.properties"})
@Order(HIGHEST_PRECEDENCE)
public class ParkOpenApiApplication {
	
	@Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
		List<MediaType> supportedMediaTypes=new ArrayList<MediaType>();
		supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
       FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
       fastConverter.setSupportedMediaTypes(supportedMediaTypes);
       
       FastJsonConfig fastJsonConfig = new FastJsonConfig();

       
       fastConverter.setFastJsonConfig(fastJsonConfig);
       
       HttpMessageConverter<?> converter = fastConverter;
       HttpMessageConverters httpConverter= new HttpMessageConverters(converter);
       return httpConverter;

    }
	
	/**
	 * @return
	 * cors  支持
	 */
	@Bean
	public FilterRegistrationBean filterSimpleCORSFilterBean() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		SimpleCORSFilter corsFilter = new SimpleCORSFilter();
		registrationBean.setFilter(corsFilter);
		List<String> urlPatterns = new ArrayList<String>();
		urlPatterns.add("/parking/calRules");
		registrationBean.setUrlPatterns(urlPatterns);
		registrationBean.setOrder(Integer.MAX_VALUE);
		return registrationBean;
	}
	
	public static void main(String[] args) throws Exception {
		SpringApplication.run(
				ParkOpenApiApplication.class,
				args);
	}

//	@Override
//	protected SpringApplicationBuilder configure(
//			SpringApplicationBuilder application) {
//		return application
//				.sources(ParkOpenApiApplication.class);
//	}

}
